package com.nbpt.db;

import android.content.Context;
import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.support.annotation.Nullable;
import android.database.Cursor;
import android.util.Log;

public class UserDBhelp extends SQLiteOpenHelper {

    private Context Context;
    private final static String DB_NAME = "user.db";
    private final static String TABLE_NAME = "tb_accounts";
    public final static int VERSION_1 = 1;
    private final String sql_create_table ="create table tb_accounts\n" +
            "(\n" +
            "id integer primary key AUTOINCREMENT, \n" +
            "user varchar(25) unique, \n" +
            "password varchar(25),\n" +
            "email text,\n" +
            "admin int\n" +
            ");";

    public UserDBhelp(@Nullable Context context,  int version) {
        super(context,DB_NAME,null,  version);
        this.Context = context;
    }

    public Cursor select(String where, String orderby) {
        StringBuilder stringBuilder = new StringBuilder(" SELECT * FROM " + TABLE_NAME);
        if (where != null) {
            stringBuilder.append(" WHERE ");
            stringBuilder.append(where);
        }
        if (orderby != null) {
            stringBuilder.append(" ORDER BY ");
            stringBuilder.append(orderby);
        }
        Log.d("DB",stringBuilder.toString());
        return  getWritableDatabase().rawQuery(stringBuilder.toString(),null);
    }

    public UserDBhelp(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version, @Nullable DatabaseErrorHandler errorHandler) {
        super(context, name, factory, version, errorHandler);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL(sql_create_table);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}
